package resources

import (
	"encoding/json"
	"errors"
	"gluenet/pkg/apis"
	"gluenet/pkg/gerrors"
	"gluenet/pkg/meta"
	"gluenet/pkg/ops"
)

func GraphLinkerd(ctx *apis.GlueContext) {
	var data *meta.Dag
	if err := ctx.DataBase.DagGet(ops.DagLinkerd, &data); err != nil {
		if errors.Is(err, gerrors.DataNotFind) {
			ctx.WebContext.JSON(apis.ResponseOK(nil))
		}
		n, _ := ctx.WebContext.JSON(apis.ResponseReqErr(err, nil))
		ctx.Logger.Errorf("request response byte:%v config nodes err: %v", n, err)
		return
	}

	var tree = new(interface{})
	json.Unmarshal([]byte(data.InstanceDag), &tree)

	ctx.WebContext.JSON(apis.ResponseOK(tree))
}

func Graph(ctx *apis.GlueContext) {
	//var data = []struct {
	//	UpdateTime  string `json:"update_time" yaml:"update_time"`
	//	InstanceDag string `json:"instances_dag" yaml:"instances_dag" xorm:"instances_dag"`
	//}{}

	var data *meta.Dag
	if err := ctx.DataBase.DagGet(ops.DagJaeger, &data); err != nil {
		if errors.Is(err, gerrors.DataNotFind) {
			ctx.WebContext.JSON(apis.ResponseOK(nil))
		}
		n, _ := ctx.WebContext.JSON(apis.ResponseReqErr(err, nil))
		ctx.Logger.Errorf("request response byte:%v config nodes err: %v", n, err)
		return
	}

	var tree = new(interface{})
	json.Unmarshal([]byte(data.InstanceDag), &tree)

	ctx.WebContext.JSON(apis.ResponseOK(tree))
	//
	//str := []byte(dag)
	//var data = new(interface{})
	//err := json.Unmarshal(str, data)
	//if err != nil {
	//	n, _ := ctx.WebContext.JSON(apis.ResponseReqErr(err, nil))
	//	ctx.Logger.Errorf("request response byte:%v config nodes err: %v", n, err)
	//	return
	//}
	//ctx.WebContext.JSON(apis.ResponseOK(data))
}

const dag = `
{
	"bbbb-aaaa-dddd-cccc": {
		"type": "application",
		"dag_nodes": {
			"web": {
				"agent": {
					"type": "host",
					"name": "node38",
					"guid": "aaaa-bbbb-cccc-dddd"
				},
				"type": "container",
				"info": {
					"Image": "nginx",
					"ContainerId": "31231231231233213123123123123123123123"
				}
			},
			"impl": {
				"agent": {
					"type": "host",
					"name": "node38",
					"guid": "aaaa-bbbb-cccc-dddd"
				},
				"type": "container",
				"info": {
					"Image": "mysql",
					"ContainerId": "fadsf312312312312332131231231231231231"
				}
			},
			"app-server": {
				"agent": {
					"type": "host",
					"name": "node39",
					"guid": "6yfd-fsdf-dfsf-342g"
				},
				"type": "container",
				"info": {
					"Image": "my-app",
					"ContainerId": "pokje231231233213123123ijhg23123123123"
				}
			}
		},
		"dag_link": [
			{
				"source": "web",
				"target": "impl",
				"value": 10,
				"info": {}
			},
			{
				"source": "web",
				"target": "app-server",
				"value": 15,
				"info": {}
			},
			{
				"source": "app-server",
				"target": "impl",
				"value": 5,
				"info": {}
			}
		]
	},
	"fsdf-5g67-5ydf-6yhg": {
		"type": "application",
		"dag_nodes": {
			"web": {
				"agent": {
					"type": "k8scluster",
					"name": "k8s-worker01",
					"guid": "aaaa-bbbb-cccc-dddd"
				},
				"type": "container",
				"info": {
					"Image": "nginx",
					"ContainerId": "31231231231233213123123123123123123123"
				}
			},
			"impl": {
				"agent": {
					"type": "k8scluster",
					"name": "k8s-worker01",
					"guid": "aaaa-bbbb-cccc-dddd"
				},
				"type": "container",
				"info": {
					"Image": "mysql",
					"ContainerId": "fadsf312312312312332131231231231231231"
				}
			},
			"server": {
				"agent": {
					"type": "k8scluster",
					"name": "k8s-master",
					"guid": "6yfd-fsdf-dfsf-342g"
				},
				"type": "container",
				"info": {
					"Image": "my-app",
					"ContainerId": "pokje231231233213123123ijhg23123123123"
				}
			}
		},
		"dag_link": [
			{
				"source": "web",
				"target": "impl",
				"value": 10,
				"info": {}
			},
			{
				"source": "web",
				"target": "app-server",
				"value": 15,
				"info": {}
			},
			{
				"source": "app-server",
				"target": "impl",
				"value": 5,
				"info": {}
			}
		]
	}
}
`
